<template>
  <div class="list">
    <div class="content">
      <h3>日志列表</h3>
      <el-button type="success" size="medial" round><i class="el-icon-folder-opened"></i>导出</el-button>
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          label="操作日期"
        prop="createTime"
          :formatter="formatDate">

        </el-table-column>
        <el-table-column
          prop="adminInfo.adminName"
          label="操作人">
        </el-table-column>
        <el-table-column
          prop="ip"
          label="ip地址"
          show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          label="操作"
          show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-if="scope.row.opt == 1">登录</span>
            <span v-if="scope.row.opt == 2">退出</span>
          </template>
        </el-table-column>
      </el-table>
      <div style="float: right;margin-top: 5px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[5, 50, 100]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
      <div style="clear: both"></div>
    </div>
  </div>
</template>

<script>
var all = []
export default {
  name: "accountList",
  created(){
    this.axios.get("micro-sys-server/opt/all").then( res => {

      let datas = res.data.data
      all = res.data.data
      this.total = datas.length
      if(this.total>5){
        for (let i=0;i<5;i++){
          this.tableData.push(datas[i])
        }
      }else {
        for (let i=0;i<datas.length;i++){
          this.tableData.push(datas[i])
        }
      }
      this.loading = false
    });},
    data(){
      return {
        tableData: [],
        multipleSelection: [],
        currentPage4: 1,
        total: 0 ,
        size: 5,
        loading: true
      }
    },
  methods:{
    //时间格式化
    formatDate:function(row, column) {
      // 获取单元格数据
      let data = row[column.property]
      if (data == null) {
        return ''
      }
      let dt = new Date(data)
      return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds()
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.tableData=[]
      this.currentPage4 = 1
      if(all.length < val){
        for (let i=0;i<all.length;i++){
          this.tableData[i] = all[i]
        }
      }else {
        for (let i=0;i<val;i++){
          this.tableData[i] = all[i]
        }
      }
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.tableData=[]
      let j = this.size
      let start = j * (val - 1 )
      if(all.length > val*this.size){
        for (let i=0;i<j;i++){
          this.tableData[i] = all[i+start]
        }
      }else {
        for (let i=0;i<all.length-start;i++){
          this.tableData[i] = all[i+start]
        }
      }
    }
  }
}
</script>

<style scoped>
.list{
  /*height: 400px;*/
  border: 1px solid white;
  width: 96%;
  margin: auto;
  padding-left: 20px;
  padding-bottom: 20px;
  box-sizing: border-box;
  background-color: white;
}
</style>
